package live.jipiao.dao;

import java.util.TreeMap;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import live.common.AirParam;
import live.common.Format;
import live.common.Verifystring;
import live.jipiao.entity.AirOrder;
public class OrderPay {

	@SuppressWarnings("unchecked")
	
	public static void main(String[] args) {
		System.out.println(new OrderPay().autoPay("B11230230021983128"));
	}
//	
	public boolean autoPay(String orderNo) {
		
		XmlFormart xfm = new XmlFormart();

		TreeMap treemap = xfm.returnPublicPostData(AirParam.ORDER_AUTOPAY);

		treemap.put("orderNo", orderNo);

		treemap.put("autopaymode", "1");

		treemap.put("paytype", "A");

		treemap.put("sign", new Verifystring().returnAirMd5Hex(treemap));

		String response = WebServicesSub.getInstance().returnResult(treemap);
		System.out.println(response);
		return readerPayResult(response,orderNo);

	}
	
	private boolean readerPayResult(String xmlString,String orderNo) {

		boolean flag = true;

		try {

			Document doc = DocumentHelper.parseText(xmlString);

			Element element = doc.getRootElement();
			
			String is_success = element.elementText("is_success");

			if(is_success.trim().equals("T")){
				
			String pay_status = element.element("response").elementText(
					"Pay_Status");
				if(pay_status.equals("F")){
					flag = false;
				}
			}else{
				flag = false;
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	public static String returnFlightSubject(AirOrder order) {
//		return new StringBuffer("机票订单-").append(Format.getInstance().returnFormatString(order.getSdate().toString(), "yyyy-MM-dd")).append(",")
//			.append(order.getScity()).append("-").append(order.getEcity())
//				.toString();
		return "";
	}

	/**
	 * 订单描述
	 * @param order
	 * @return
	 */
	public String returnFlightBody(AirOrder order) {
		return new StringBuffer(order.getFlightNo().toString()).append(",")
				.append(order.getScityName()).append("-").append(
						order.getEcityName()).append(",").append(
						order.getSdate()).append(",")
				.append(order.getDepTime()).append(",").append(
						order.getArrTime()).toString();
		
	}
	
}
